package com.encryption.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Expression;

import com.encryption.model.HibernateSessionFactory;


/**
 * Data access object (DAO) for domain model
 * @author MyEclipse Persistence Tools
 */
public class BaseHibernateDAO implements IBaseHibernateDAO {
	
	public Session getSession() {
		return HibernateSessionFactory.getSession();
	}
	/**
	 * 设置criteria
	 * @param conditions
	 * @param criteria
	 */
	public static void addCriteria(List<Condition> conditions,Criteria criteria){
		for (int i = 0; i < conditions.size(); i++) {
			//=,>,>=,<,<=,like,between
			if(Condition.EQ.equals(conditions.get(i).getOperation())){
				criteria.add(Expression.eq(conditions.get(i).getCode(),conditions.get(i).getValue()));
				
			}else if(Condition.GT.equals(conditions.get(i).getOperation())){
				criteria.add(Expression.gt(conditions.get(i).getCode(),conditions.get(i).getValue()));
				
			}else if(Condition.GE.equals(conditions.get(i).getOperation())){
				criteria.add(Expression.ge(conditions.get(i).getCode(),conditions.get(i).getValue()));
				
			}else if(Condition.LT.equals(conditions.get(i).getOperation())){
				criteria.add(Expression.lt(conditions.get(i).getCode(),conditions.get(i).getValue()));
				
			}else if(Condition.LE.equals(conditions.get(i).getOperation())){
				criteria.add(Expression.le(conditions.get(i).getCode(),conditions.get(i).getValue()));
				
			}else if(Condition.LIKE.equals(conditions.get(i).getOperation())){
				criteria.add(Expression.like(conditions.get(i).getCode(),"%"+conditions.get(i).getValue()+"%"));
				
			}
			else if(Condition.BETWEEN.equals(conditions.get(i).getOperation())){
				criteria.add(Expression.between(conditions.get(i).getCode(),((List)conditions.get(i).getValue()).get(0),
						((List)conditions.get(i).getValue()).get(1)));
			}
		}
	}
}